package com.goldcn.goods.controller;

import com.goldcn.common.util.UuidUtils;
import com.goldcn.config.BankbaseCfg;
import com.goldcn.goods.service.ExcelImageService;
import com.goldcn.goods.service.GoodsService;
import com.goldcn.goods.vo.GoodsInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


@RestController
@Api(value = "裸石xls导入")
@RequestMapping(value = "/xls")
public class ImportController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    BankbaseCfg bankbaseCfg;


    @Autowired
    ExcelImageService excelImageService;


    //裸钻
    private String[][] skuKeys = {{"hxShape", "hxWeight", "hxColor", "hxNeatness", "hxCut", "paoguang", "duichengxing", "yingguanxing", "certOrg", "cert", "price"}, {"shape", "weight", "color"}};
    private String[][] skuNames = {{"形状", "重量", "颜色", "净度", "切工", "抛光", "对称", "荧光", "鉴定所", "证书号", "价格"}, {"shape", "weight", "color"}};

    @ApiOperation(value = "从xls导入数据", notes = "fileName为上传返回的xls文件,GoodsInfoVo基本商品信息")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ResponseBody
    public Object importFromXls(@RequestBody GoodsInfoVo goodsInfoVo, @RequestParam(value = "fileName", required = false) String fileName) throws Exception {
        if (goodsInfoVo.getClassifyName().indexOf("贵") >= 0) {
            return excelImageService.importXlsLD(fileName, goodsInfoVo);
        } else if (goodsInfoVo.getSubClassifyName().indexOf("钻") >= 0) {
            return excelImageService.importXlsHX(fileName, goodsInfoVo);
        }
        return "无数据导入";
    }


    /**
     * 钻石批量上传为商品
     *
     * @param
     * @param
     * @param file
     * @param type 1.钻石 2.蓝宝石
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/file", produces = "text/plain")
    @ApiOperation(value = "上传xls", notes = "上传xls文件，成功返回xls文件名")
    public void uploadfile(HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file,
                           @RequestParam(value = "type", required = false) String type) throws Exception {
        FileOutputStream fos = null;
        String fileName = "";
        try {
            //系统指定原图文件名 加入上传待处理List集合
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            fileName = UuidUtils.uuid() + suffix;
            String filePath = "." + File.separator + fileName;

            fos = new FileOutputStream(filePath);
            fos.write(file.getBytes()); // 写入文件
            fos.flush();

        } catch (Exception se) {
            se.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        JSONObject json = new JSONObject();
        json.put("name", fileName);
        //return json.toString();

        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Charset", "utf-8");
        response.setHeader("Cache-Control", "no-cache");

        response.getWriter().write(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
    }


//    @RequestMapping(value = "/importxls2", method = RequestMethod.POST)
//    @ResponseBody
//    public Object operateGoodsSkuPriceShop2(@RequestBody JSONObject jsonxls) throws Exception {
//        String fileName = jsonxls.getString("fileName");
//        String filePath = "." + File.separator + fileName;
//        ImportExcel importExcel = new ImportExcel(filePath);
//        Object[][] data = importExcel.getObj();
//
//        for (int i = 0; i < data.length; i++) {
//
//
//        }
//        return "success";
//    }


//    @RequestMapping(value = "/importxls", method = RequestMethod.POST)
//    @ResponseBody
//    public Object operateGoodsSkuPriceShop(@RequestBody JSONObject jsonxls) throws Exception {
//        String fileName = jsonxls.getString("fileName");
//        String type = jsonxls.getString("type");
//
//        String filePath = "." + File.separator + fileName;
//        GoodsDetailsVo goodsDetailsVo = new GoodsDetailsVo();
//        goodsDetailsVo.setOperateType(OperateTypeEnum.insert);
//
//        List<String> imageUrlList = new ArrayList<String>();
//        imageUrlList.add(jsonxls.getString("imgName"));
//        goodsDetailsVo.setImageUrlList(imageUrlList);
//        List<GoodsSku> goodsSkuList = new ArrayList<>();
//        GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
//
//        int skuIdx = 0;
//
//        goodsInfoVo.setLabelID(Long.parseLong(jsonxls.getString("LabelID")));
//        goodsInfoVo.setSupplierID(Long.parseLong(jsonxls.getString("SupplierID")));
//        goodsInfoVo.setSupplierName(jsonxls.getString("SupplierName"));
//        goodsInfoVo.setSubClassifyID(jsonxls.getString("SubClassifyID"));
//        goodsInfoVo.setSubClassifyName(jsonxls.getString("SubClassifyName"));
//        goodsInfoVo.setClassifyID(jsonxls.getString("ClassifyID"));
//        goodsInfoVo.setClassifyName(jsonxls.getString("ClassifyName"));
//        String desc = jsonxls.getString("desc");
//        Integer inventory = Integer.parseInt(jsonxls.getString("inventory"));
//
//        Integer count = Integer.parseInt(jsonxls.getString("count"));
//
//        String title = jsonxls.getString("title");
//        goodsInfoVo.setTitle(title);
//        goodsInfoVo.setName(title);
//        goodsInfoVo.setPrdNo(title);
//        goodsInfoVo.setGoodsPCDesc("<html><head lang=\\\"en\\\"><meta name=\\\"viewport\\\" content=\\\"width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0\\\"><meta content=\\\"telephone=no\\\" name=\\\"format-detection\\\"><meta name=\\\"apple-mobile-web-app-capable\\\" content=\\\"yes\\\"><meta content=\\\"\\\" name=\\\"keywords\\\"><meta content=\\\"\\\" name=\\\"description\\\"><meta name=\\\"viewport\\\" content=\\\"width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1,maximum-scale=1\\\"><meta name=\\\"apple-mobile-web-app-capable\\\" content=\\\"yes\\\"><meta name=\\\"apple-mobile-web-app-status-bar-style\\\" content=\\\"black\\\"><style type=\\\"text/css\\\">* {margin: 0;padding: 0}body {position: relative;font: 14px \\\"microsoft yahei\\\";}</style></head><body><p>" + desc + "<br/></p></body></html>");
//        goodsInfoVo.setGoodsAPPDesc("<p>" + desc + "<br/></p>");
//        if ("1".equalsIgnoreCase(type)) {
//
//        } else {
//            skuIdx = 1;
//
//        }
//
//
//        ImportExcel importExcel = new ImportExcel(filePath);
//        Object[][] data = importExcel.getObj();
//        StringBuffer tmpName = new StringBuffer();
//        StringBuffer skuName = new StringBuffer();
//
//        for (int i = 0; i < data.length; i++) {
//            JSONObject jsonObject = new JSONObject();
//            GoodsStoneDto stoneDto = new GoodsStoneDto();
//            for (int j = 0; j < data[i].length - 1; j++) {
//                jsonObject.put(skuKeys[skuIdx][j], data[i][j]);
//                skuName.append(skuNames[skuIdx][j] + ":" + data[i][j] + " ");
//                if (skuKeys[skuIdx][j].equalsIgnoreCase("hxShape")) {
//                    stoneDto.setHxShape(data[i][j].toString());
//                    tmpName.append(stoneDto.getHxShape() + " ");
//                } else if (skuKeys[skuIdx][j].equalsIgnoreCase("hxWeight")) {
//                    stoneDto.setHxWeight(data[i][j].toString());
//                    tmpName.append(stoneDto.getHxWeight() + " ");
//                } else if (skuKeys[skuIdx][j].equalsIgnoreCase("hxSize")) {
//                    stoneDto.setHxSize(data[i][j].toString());
//                } else if (skuKeys[skuIdx][j].equalsIgnoreCase("hxCut")) {
//                    stoneDto.setHxCut(data[i][j].toString());
//                } else if (skuKeys[skuIdx][j].equalsIgnoreCase("hxColor")) {
//                    stoneDto.setHxColor(data[i][j].toString());
//                } else if (skuKeys[skuIdx][j].equalsIgnoreCase("hxNeatness")) {
//                    stoneDto.setHxNeatness(data[i][j].toString());
//                }
//            }
//
//            jsonObject.put("name", skuName.toString());
//
//            GoodsSku goodsSku = new GoodsSku();
//            goodsSku.setAttrJson(jsonObject.toString());
//            goodsSku.setInventory(inventory);
//            goodsSku.setPrice(new BigDecimal((String) data[i][data[i].length - 1]));
//            goodsSku.setWholeSalePrice(new BigDecimal((String) data[i][data[i].length - 1]));
//
//            goodsSkuList.add(goodsSku);
//            goodsDetailsVo.setGoodsSkuList(goodsSkuList);
//            goodsInfoVo.setTitle(tmpName.toString());
//
//            goodsDetailsVo.setGoodsInfoVo(goodsInfoVo);
//            goodsService.importGoods(goodsDetailsVo, stoneDto, goodsSku);
//
//            tmpName.setLength(0);
//            skuName.setLength(0);
//
//            goodsSkuList.clear();
//
//            if (count == 1) {
//                break;
//            }
//        }
//        return "success";
//    }
//
//
//    /**
//     * 高级搜索商品属性类型定义
//     *
//     * @return
//     */
//    @RequestMapping(value = "/search/clazzAttrList", method = RequestMethod.GET)
//    public Object getPlatformAttrList() {
//        ClazzObjDto clazzObjDto = new ClazzObjDto();
//        List<String> list = new ArrayList<String>();
//        String ids = bankbaseCfg.getGoodsClazzIDs();
//        if (StringUtils.isNotEmpty(ids)) {
//            String[] clazzStr = ids.split(",");
//            for (int i = 0; i < clazzStr.length; i++) {
//                list.add(clazzStr[i]);
//            }
//        }
//
//        clazzObjDto.setClazzObjID(list);
//        return goodsService.getPlatformAttrList(clazzObjDto);
//    }


}
